package cn.edu.zut.salarymanagesystem.adapter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.edu.zut.salarymanagesystem.R;
import cn.edu.zut.salarymanagesystem.bean.pojo.Item;
import cn.edu.zut.salarymanagesystem.bean.pojo.WorkRecord;

public class ItemAdapter extends BaseAdapter {
    private List<Item> items;

    private Context context;

    private LayoutInflater inflater;
    private static final String TAG = "d8g";

    public ItemAdapter(List<Item> items, Context context) {
        this.items = items;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.line_item, null);
            viewHolder.txtItemName = convertView.findViewById(R.id.text_item_name);
            viewHolder.txtCommentary = convertView.findViewById(R.id.text_commentary);
            viewHolder.txtPrice = convertView.findViewById(R.id.text_price);
            convertView.setTag(viewHolder);
        } else {
            Log.i(TAG, "getView !=null else: " + convertView);
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            viewHolder.txtItemName = convertView.findViewById(R.id.text_item_name);
            viewHolder.txtCommentary = convertView.findViewById(R.id.text_commentary);
            viewHolder.txtPrice = convertView.findViewById(R.id.text_price);
            viewHolder.txtItemName.setText(items.get(i).getObjectName() == null ? "为空" : items.get(i).getObjectName());
            viewHolder.txtCommentary.setText(items.get(i).getCommentary() == null ? "无" : items.get(i).getCommentary());
            viewHolder.txtPrice.setText(String.valueOf(items.get(i).getValue()));
        } catch (Exception e) {
            Log.e(TAG, "getView: exception" + e.getMessage());
        }
        Log.i(TAG, "getView3: " + convertView);
        return convertView;
    }

    private static class ViewHolder {
        public TextView txtItemName;
        public TextView txtCommentary;
        public TextView txtPrice;

        public ViewHolder() {
        }
    }
}
